public class syn {
    private static int count = 0;
    public static Object locker = new Object();
    public static Object locker2 = new Object();

    public static void main(String[] args) throws InterruptedException {
        // 把count变成局部变量，会直接报错
        // int count=0;
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 50000; i++) {

                synchronized (locker) {// 得用Object类型作为锁
                    count++;
                }

            }
        });

        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 50000; i++) {
                   synchronized (locker2) {// 得用Object类型作为锁
                    count++;
                }

            }
        });

        t1.start();
        t2.start();

        t1.join();
        t2.join();
        System.out.println(count);
    }

}
